package com.yxjx.duoxue.customview;

import java.util.ArrayList;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.course.CourseIntroductionActivity;
import com.yxjx.duoxue.course.TeacherIntroductionActivity;
import com.yxjx.duoxue.datamodel.DMTeacher;
import com.yxjx.duoxue.datamodel.course.DMCourse;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * Foldable ListView which embbed in scrollview.
 * @author mengmengchen
 *
 */
public class YxFoldableListView extends ListView {

	public YxFoldableListView(final Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Adapter adapter = getAdapter();
				if (adapter instanceof FoldableAdapter && adapter.getItemViewType(arg2) == FoldableAdapter.TYPE_COURSE_ITEM)  {
					FoldableAdapter<DMCourse> foldeableAdapter = (FoldableAdapter<DMCourse>)adapter;
					DMCourse course = foldeableAdapter.getData().get(arg2);
					
					Intent intent = new Intent(context, CourseIntroductionActivity.class);
					intent.putExtra(NwUtils.KEY_ID, "" + course.getCourseId());
					context.startActivity(intent);
				}
			}
		});
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(   
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}
	
	public class FoldableAdapter<E> extends BaseAdapter {
		
		public static final int TYPE_COURSE_ITEM = 0;
		public static final int TYPE_FOLDABLE_BUTTON = 1;
		public static final int TYPE_COUNT = 2;
		
		private static final int FOLDABLE_BUTTON_NUM = 1;
		private static final int DEFAULT_SHOWN_NUM = 3;
		
		private int mShownRecordNum = 0;
		private ArrayList<E> mData;
		
		private boolean mIsCurrentFoldedClose = true;
		private boolean mShowFoldableButton = true;
		
		public ArrayList<E> getData() {
			return mData;
		}
		
		public void setData(ArrayList<E> data) {
			mShownRecordNum = data == null ? 0 : data.size();
			mData = data;
			mShowFoldableButton = (mShownRecordNum > DEFAULT_SHOWN_NUM) ? true : false;
			notifyDataSetChanged();
		}
		
		@Override
		public int getViewTypeCount() {
			return TYPE_COUNT;
		}
		
		@Override
		public int getItemViewType(int position) {
			if (mShowFoldableButton) {
				return (position != getCount() - 1) ? TYPE_COURSE_ITEM : TYPE_FOLDABLE_BUTTON;
			} else {
				return TYPE_COURSE_ITEM;
			}
		}

		@Override
		public int getCount() {
			int temp = 0;
			if (mShownRecordNum > DEFAULT_SHOWN_NUM && mIsCurrentFoldedClose) {
				temp = DEFAULT_SHOWN_NUM;
			} else {
				temp = mShownRecordNum;
			}
			return temp + (mShowFoldableButton ? FOLDABLE_BUTTON_NUM : 0);
		}
		
		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret = null;
			if (getItemViewType(arg0) == TYPE_COURSE_ITEM) {
				
				if (arg1 == null) {
					ret = LayoutInflater.from(getContext()).inflate(
							R.layout.item_list_course, null);
				} else {
					ret = arg1;
				}
				
				DMCourse course = (DMCourse) mData.get(arg0);
				UIHelper.setText(ret, R.id.course_name, course.getCourseName());
				UIHelper.setText(ret, R.id.category_and_requirement,
						Utils.getCategoryText(course));
				UIHelper.asyncLoadImage(
						(ImageView) ret.findViewById(R.id.course_icon),
						course.getHeadImageUrl(), R.drawable.default_icon);
				
			} else if (getItemViewType(arg0) == TYPE_FOLDABLE_BUTTON) {
				if (arg1 == null) {
					ret = LayoutInflater.from(getContext()).inflate(R.layout.item_foldable_button, null);
					ret.setOnClickListener(mClickListener);
				} else {
					ret = arg1;
				}
				String text = getResources().getString(R.string.foldable_open);
				text = text.replace("N", "" + mShownRecordNum);
				UIHelper.setText(ret, R.id.foldable_text, !mIsCurrentFoldedClose ? getResources().getString(R.string.foldable_close) :
					text);
			}
			return ret;
		}
		
		@Override
		public Object getItem(int arg0) {
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}
		
		private View.OnClickListener mClickListener = new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				switch (arg0.getId()) {
				case R.id.foldable_button:
					mIsCurrentFoldedClose = !mIsCurrentFoldedClose;
					arg0.findViewById(R.id.foldable_indicator).setSelected(!mIsCurrentFoldedClose);
					notifyDataSetChanged();
					break;
				default:
					break;
				}
			}
		};
	}
}
